package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.mall.domain.bo.GoodsSpecificationsDetailBo;
import com.ruoyi.mall.domain.vo.GoodsSpecificationsDetailVo;
import com.ruoyi.mall.service.IGoodsSpecificationsDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.bo.GoodsSpecificationsBo;
import com.ruoyi.mall.domain.vo.GoodsSpecificationsVo;
import com.ruoyi.mall.domain.GoodsSpecifications;
import com.ruoyi.mall.mapper.GoodsSpecificationsMapper;
import com.ruoyi.mall.service.IGoodsSpecificationsService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品规格Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@RequiredArgsConstructor
@Service
public class GoodsSpecificationsServiceImpl implements IGoodsSpecificationsService {

    private final GoodsSpecificationsMapper baseMapper;
    @Autowired
    private IGoodsSpecificationsDetailService goodsSpecificationsDetailService;

    /**
     * 查询商品规格
     */
    @Override
    public GoodsSpecificationsVo queryById(Long goodsSpecificationsId){
        return baseMapper.selectVoById(goodsSpecificationsId);
    }

    /**
     * 查询商品规格列表
     */
    @Override
    public TableDataInfo<GoodsSpecificationsVo> queryPageList(GoodsSpecificationsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GoodsSpecifications> lqw = buildQueryWrapper(bo);
        Page<GoodsSpecificationsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品规格列表
     */
    @Override
    public List<GoodsSpecificationsVo> queryList(GoodsSpecificationsBo bo)
    {
        LambdaQueryWrapper<GoodsSpecifications> lqw = buildQueryWrapper(bo);
        List<GoodsSpecificationsVo> lstGoodsSpecificationsVo = baseMapper.selectVoList(lqw);
        for (GoodsSpecificationsVo goodsSpecificationsVo : lstGoodsSpecificationsVo)
        {
            GoodsSpecificationsDetailBo query = new GoodsSpecificationsDetailBo();
            query.setGoodsSpecificationsId(goodsSpecificationsVo.getGoodsSpecificationsId());
            List<GoodsSpecificationsDetailVo> lstGoodsSpecificationsDetailVo = goodsSpecificationsDetailService.queryList(query);
            if (goodsSpecificationsVo.getLstGoodsSpecificationsDetailVo() == null)
            {
                goodsSpecificationsVo.setLstGoodsSpecificationsDetailVo(new ArrayList<>());
            }
            goodsSpecificationsVo.getLstGoodsSpecificationsDetailVo().addAll(lstGoodsSpecificationsDetailVo);
        }
        return lstGoodsSpecificationsVo;
    }

    private LambdaQueryWrapper<GoodsSpecifications> buildQueryWrapper(GoodsSpecificationsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GoodsSpecifications> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoodsId() != null, GoodsSpecifications::getGoodsId, bo.getGoodsId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), GoodsSpecifications::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDesc()), GoodsSpecifications::getDesc, bo.getDesc());
        lqw.eq(bo.getYn() != null, GoodsSpecifications::getYn, bo.getYn());
        return lqw;
    }

    /**
     * 新增商品规格
     */
    @Override
    public Boolean insertByBo(GoodsSpecificationsBo bo) {
        GoodsSpecifications add = BeanUtil.toBean(bo, GoodsSpecifications.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGoodsSpecificationsId(add.getGoodsSpecificationsId());
        }
        return flag;
    }

    /**
     * 修改商品规格
     */
    @Override
    public Boolean updateByBo(GoodsSpecificationsBo bo) {
        GoodsSpecifications update = BeanUtil.toBean(bo, GoodsSpecifications.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GoodsSpecifications entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品规格
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id :ids)
        {
            GoodsSpecificationsDetailBo query = new GoodsSpecificationsDetailBo();
            query.setGoodsSpecificationsId(id);
            List<GoodsSpecificationsDetailVo> lstSpecificationsVoList = goodsSpecificationsDetailService.queryList(query);
            Collection<Long> collection = new ArrayList<>();
            for (GoodsSpecificationsDetailVo goodsSpecificationsDetailVo: lstSpecificationsVoList)
            {
                collection.add(goodsSpecificationsDetailVo.getGoodsSpecificationsDetailId());
            }
            goodsSpecificationsDetailService.deleteWithValidByIds(collection, true);
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
